<template>
  <div
    class='container'
  >
    <el-card class="box-card">
      <el-button
        class="button"
        icon="el-icon-edit"
        type="success"
        @click="addArticle"
      >
        新增技巧
      </el-button>
      <el-form :inline="true" label-width="90px">
        <el-form-item label="关键字">
            <el-input placeholder="请输入" />
          </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="sizeForm.state" placeholder="请选择活动区域">
            <el-option label="启用" value="shanghai"></el-option>
            <el-option label="禁用" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item size="large">
          <el-button type="primary">搜索</el-button>
          <el-button>清除</el-button>
        </el-form-item>
      </el-form>
      <el-alert
        :title="'共'+ articlesList.counts + '条记录'"
        type="info"
        show-icon
        class="record"
      >
      </el-alert>
      <my-table
        :table-props="{
          data: articlesList.items,
          border: true
        }"
        :columns="[
          { label: '试题编号', prop: 'index', width: 100, align: 'center' },
          { label: '文章标题', prop: 'title', width: 200, align: 'center' },
          { label: '阅读数', prop: 'visits', width: 150, align: 'center' },
          { label: '录入人', prop: 'username', width: 150, align: 'center' },
          { label: '录入时间', prop: 'createTime', width: 150, align: 'center' },
          { label: '状态', prop: 'state', width: 300, align: 'center' },
          { label: '操作', slotName: 'operators', width: 300, align: 'center' }, // 带插槽
        ]"
        :pagination-props="{
          layout: 'total, pager',
          total: articlesList.counts
        }"
        :pagination-events="events"
      >
        <template v-slot:operators="scope">
          <el-button type="text">预览</el-button>
          <el-button @click="forbid(scope.row)" type="text">{{ scope.row.state === '已禁用' ? '启用' : '禁用' }}</el-button>
          <el-button type="text" :disabled="scope.row.state === '已禁用' ? false : true ">修改</el-button>
          <el-button @click="deleteArticle(scope.row)" type="text" :disabled="scope.row.state === '已禁用' ? false : true ">删除</el-button>
        </template>
      </my-table>
    </el-card>
    <el-dialog
      title="新增文章"
      :visible.sync="addUserVisible"
      width="30%"
      @close="onAddEmployeeClose"
    >
      <el-form
        :model="addArticlesGoTo"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="120px"
        class="demo-ruleForm"
        style="width: 100%; margin-left: 10px;"
      >
        <el-form-item
          label="文章标题"
          prop="title"
        >
          <el-input
            type="text"
            v-model="addArticlesGoTo.title"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item
          label="文章内容"
            prop="articleBody"
        >
          <el-input
            type="text"
            v-model="addArticlesGoTo.articleBody"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item
          label="视频地址"
            prop="videoURL"
        >
          <el-input
            type="text"
            v-model="addArticlesGoTo.videoURL"
            autocomplete="off"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onAddEmployeeClose">取 消</el-button>
        <el-button type="primary" @click="onAddEmployeeOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import * as articlesApi from '@/api/hmmm/articles'
export default {
  data() {
    const validateNull = (rule, value, callback) => {
      if (!value.trim()) {
        callback(new Error(this.$t('值不能为空')))
      } else {
        callback()
      }
    }
    return {
      currentPage: 1,
      pageSize: 10,
      events: {
        'current-change': this.onCurrentPageChange
      },
      sizeForm: {
        nameList: '',
        state: '',
      },
      addUserVisible: false,
      addArticlesGoTo: {
        title: '',
        articleBody: '',
        videoURL: '',
      },
      addFormRules: {
        title:[{ required: true, trigger: 'blur', validator: validateNull }],
        articleBody:[{ required: true, trigger: 'blur', validator: validateNull }],
        videoURL:[{ required: true, trigger: 'blur', validator: validateNull }],
      }
    }
  },
  computed: {
    ...mapState('articles', [
      'articlesList'
    ])
  },
   methods: {
    ...mapActions('articles', [
      'updateArticlesList'
    ]),
    onCurrentPageChange(currentPage) {
      this.currentPage = currentPage
      this.updateArticlesList({
        page: this.currentPage,
        size: this.pageSize,
      });
    },
    deleteArticle(row) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            articlesApi.remove({
              id: row.id
            })
            .then(() => {
              this.$message({
                type: 'success',
                message: '删除成功'
              });
            })
            this.updateArticlesList({
              page: this.currentPage,
              size: this.pageSize,
          });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
    },
    addArticle() {
      this.addUserVisible = true
    },
    async onAddEmployeeOk() {
      await articlesApi.addArticles(this.addArticlesGoTo)
        .then(msg => this.$message.success(msg))
        .catch(msg => this.$message.error(msg))
      // 请求完成后刷新列表
      this.updateArticlesList({
        page: this.currentPage,
        size: this.pageSize
      })
      this.addUserVisible = false
    },
    onAddEmployeeClose() {
      this.addUserVisible = false
    }
  },
  async created() {
    await this.updateArticlesList({
      page: this.currentPage,
      size: this.pageSize,
    });
  },
}
</script>

<style scoped lang='scss'>

.button {
  float: right;
}
.record {
  margin-bottom: 12px;
}
</style>
